package com.why.zing.alipay.config;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.why.zing.alipay.constant.Constants;
import com.why.zing.alipay.service.AlipayTradeService;
import com.why.zing.alipay.service.impl.AlipayTradeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 自动配置类，如果有支付宝属性配置即生效
 *
 * @author 睁眼看世界
 * @date 2020年1月10日
 */
@Configuration
@EnableConfigurationProperties(value = AliPayProperties.class)
@ConditionalOnClass(AliPayProperties.class)
public class AliPayClientAutoConfiguration {

    @Autowired
    private AliPayProperties aliPayProperties;

    @Bean
    @ConditionalOnMissingBean(AlipayTradeService.class)
    public AlipayTradeService alipayTradeService() {
        return new AlipayTradeServiceImpl();
    }

    @PostConstruct
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(aliPayProperties.getOpenApiDomain(), aliPayProperties.getAppid(), aliPayProperties.getPrivateKey(),
                Constants.FORMAT, Constants.CHARSET, aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
    }
}
